<?php

class Captcha {
	
	// SimplyPost required class variables
	var $description	= 'Generates Captchas for the Login and Registration Forms.';
	var $version		= '0.3';
	
	var $CI;
	var $login;
	var $register;
	
	function Captcha()
	{
		$this->CI =& get_instance();
		log_message('debug', 'Captcha plugin loaded.');
	}
	
	function install()
	{
		$query = "CREATE TABLE IF NOT EXISTS captcha(
			id INT NOT NULL AUTO_INCREMENT,
			PRIMARY KEY(id),
			login TINYINT,
			register TINYINT)";
		$this->CI->db->query($query);
		
		$data = array(
			'login' => 0,
			'register' => 0,
		);
		
		$this->CI->db->insert('captcha', $data);
		
		@mkdir('./assets/images/captcha');
	}

	function remove()
	{
		$query = "DROP TABLE IF EXISTS captcha";
		$this->CI->db->query($query);
		
		$path = './assets/images/captcha';
		
		$handle = opendir($path);
		while (false !== ($file = readdir($handle)) )
		{
			if($file != '.' && $file != '..')
				unlink($path.'/'.$file);
		}
		closedir($handle);
		
		rmdir($path);
	}
	
	function initialize()
	{
		$query = $this->CI->db->get('captcha', 1, 0);
		$tmp = $query->row_array();
				
		$this->login = $tmp['login'];
		$this->register = $tmp['register'];
	}

	function configure()
	{		
		if (isset($_POST['submit']))
		{
			$log = isset($_POST['login']) ? 1 : 0;
			$reg = isset($_POST['register']) ? 1 : 0;

			$data = array('login' => $log, 'register' => $reg);

			$this->CI->db->where('id', 1);			
			$this->CI->db->update('captcha', $data);			
			
			$this->CI->session->set_flashdata('msg', 'Captcha Preferences Changed.');
			redirect('admin/plugins');
		}
		$this->CI->load->helper('form');

		$login = $this->login;
		$register = $this->register;

		ob_start();
		?>
		<form action="" method="post" accept-charset="utf-8">
			
			<p><?= form_checkbox('login', 'enabled', $login) ?><label for="login">Login Captcha</label></p>
			<p><?= form_checkbox('register', 'enabled', $register) ?><label for="register">Register Captcha</label></p>
			
			<input type="submit" name="submit" value="Update">
		</form>
		<?php
		ob_end_flush();
	}

	function login_form()
	{
		if (!$this->login)
		{
			return '';
		}
		
		// CI Native captcha_pi
		$this->CI->load->plugin('captcha');	
		
		$vals = array(
						'img_path'	=> './assets/images/captcha/',
						'img_url'	=> 'http://dev/sp/assets/images/captcha/',
					);

		$cap = create_captcha($vals);

		$this->CI->session->set_flashdata('cap', $cap['word']);
		
		$output = '<tr><td></td><td>'.$cap['image'].'</td></tr>';
		$output .= '<tr>';
		$output .= '<td><label for="captcha">Captcha: </label></td>';
		$output .= '<td><input type="text" name="captcha" value="" id="captcha"></td>';
		$output .= '</tr>';

		return $output;
	}
	
	function process_login()
	{
		if (!$this->login)
		{
			return array(TRUE, 'no effect');
		}
		
		$saved = $this->CI->session->flashdata('cap');
		$in = $_POST['captcha'];
		
		if (strtolower($saved) != strtolower($in))
		{
			return array(FALSE, 'Captcha incorrect.');
		}
		return array(TRUE, 'placeholder');
	}
	
	function register_form()
	{
		if (!$this->register)
		{
			return '';
		}
		
		// CI Native captcha_pi
		$this->CI->load->plugin('captcha');	
		
		$vals = array(
						'img_path'	=> './assets/images/captcha/',
						'img_url'	=> 'http://dev/sp/assets/images/captcha/',
					);

		$cap = create_captcha($vals);
		
		$this->CI->session->set_flashdata('cap', $cap['word']);
		
		$output = '<tr><td></td><td>'.$cap['image'].'</td></tr>';
		$output .= '<tr>';
		$output .= '<td><label for="captcha">Captcha: </label></td>';
		$output .= '<td><input type="text" name="captcha" value="" id="captcha"></td>';
		$output .= '</tr>';

		return $output;
	}
	
	function process_register()
	{
		if (!$this->register) {
			return array(TRUE, 'no effect');
		}
		
		$saved = $this->CI->session->flashdata('cap');
		$in = $_POST['captcha'];
		
		if (strtolower($saved) != strtolower($in))
		{
			return array(FALSE, 'Captcha incorrect.');
		}
		return array(TRUE, 'placeholder');
	}
	
}